/**
 * Entry point for development and production PWA builds.
 * Packaged (NPM) builds go through `index-umd.js`
 */

import 'regenerator-runtime/runtime';
import qs from 'querystring';
import cookie from 'cookie';
import { user } from '@ohif/core';

import App from './App.js';
import React from 'react';
import ReactDOM from 'react-dom';

// test

/**
 * EXTENSIONS
 * =================
 *
 * Importing and modifying the extensions our app uses HERE allows us to leverage
 * tree shaking and a few other niceties. However, by including them here they become
 * "baked in" to the published application.
 *
 * Depending on your use case/needs, you may want to consider not adding any extensions
 * by default HERE, and instead provide them via the extensions configuration key or
 * by using the exported `App` component, and passing in your extensions as props using
 * the defaultExtensions property.
 */
import { StudyBrowserExtension, MeasurementExtension } from './appExtensions';
import { vtkMprExtension } from '@ohif/extension-vtk-mpr';
import { vtkVrExtension } from '@ohif/extension-vtk-vr';

// Add this for Debugging purposes:
//import OHIFDebuggingExtension from '@ohif/extension-debugging';
import { version } from '../package.json';

/*
 * Default Settings
 */
let config = {};

const _isChrome = () => {
  const uaData = window.navigator.userAgentData;
  if (uaData && uaData.brands && uaData.brands.length > 1) {
    return uaData.brands[1].brand === "Google Chrome";
  }
  return false;
};

if (window) {
  config = window.config || {};
  window.version = version;
  config.isMobile = window.navigator.userAgent.match(
    /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
  );
  config.isChrome = _isChrome();

  // const { availWidth, availHeight } = window.screen || {};
  // if (availWidth && availHeight) {
  //   window.resizeTo(availWidth, availHeight);
  // }

  window.onhashchange = function () {
    window.location.reload();
  };
}

config.isDev = process.env.NODE_ENV === 'development';

let { href, search } = window.location;
if (!search) {
  const i = href.indexOf('?');
  if (i > 0) {
    search = href.slice(i+1);
  }
}
const query = qs.parse(search);
config.visitFlag = query.flag;

const { xhrTokenKey, sso } = config;
const cookies = cookie.parse(document.cookie);
let token = cookies[xhrTokenKey];

if (!token) {
  token = query.token;
}

if (token) {
  // if (!cookies[xhrTokenKey]) {
  //   document.cookie = cookie.serialize(xhrTokenKey, token);
  // }
  config.xhrTokenValue = token;
  user.setTokenKey(config.xhrTokenKey);
  user.setAccessToken(token);
} else {
  if (sso && (!config.isDev)) {
    // relink to sso;
    window.location = sso + encodeURIComponent(href);
  }
}


const appProps = {
  config,
  defaultExtensions: [StudyBrowserExtension, MeasurementExtension],
};

if (!config.isMobile) {
  appProps.defaultExtensions = appProps.defaultExtensions.concat([vtkMprExtension, vtkVrExtension]);
}

/** Create App */
const app = React.createElement(App, appProps, null);

/** Render */
ReactDOM.render(app, document.getElementById('root'));
